package com.yinhe.music.yhmusic.video;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.TabAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.log.BlankUtil;
import com.yinhe.music.yhmusic.model.Tags;
import com.yinhe.music.yhmusic.utils.ViewUtils;
import com.yinhe.music.yhmusic.video.list.MvFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class VideoActivity extends BaseServiceActivity implements IVideoContract.IVideoView {
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.line)
    View line;
    private int type;

    private VideoPresenter mPresenter;

    @Override
    public void initView() {
        if (getIntent() != null) {
            type = getIntent().getIntExtra("type", IConstants.MORE_VIDEO);
        }

        line.setVisibility(View.GONE);

        if (type == IConstants.MORE_VIDEO) {
            setToolbar("视频");
        } else {
            setToolbar("MV");
        }

        showQuickControl(true);

        mPresenter.getMovieTag();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_mv_more;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new VideoPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    private void setupViewPager(List<Tags> list) {
        String[] title = new String[list.size()];
        List<Fragment> fragmentList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            MvFragment fragment = new MvFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("tags", list.get(i).getType());
            if (type == IConstants.MORE_VIDEO) {
                bundle.putInt("type", IConstants.MORE_VIDEO);
            } else {
                bundle.putInt("type", IConstants.NEW_MORE_MV);
            }
            title[i] = list.get(i).getName();
            fragment.setArguments(bundle);
            fragmentList.add(fragment);
        }

        TabAdapter adapter = new TabAdapter(getSupportFragmentManager(), fragmentList, title);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(list.size());

        if (list.size()>6){
            tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
        tabs.setupWithViewPager(viewpager);
        ViewUtils.reflexIndicator(tabs, 20);
    }


    @Override
    public void setTagUI(Tags tags) {
        List<Tags> tagsList = tags.getTags();
        if (!BlankUtil.isBlank(tagsList)) {
            setupViewPager(tagsList);
        }
    }
}
